package com.qiantu.phone.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.bigkoo.pickerview.adapter.NumericWheelAdapter;
import com.contrarywind.view.WheelView;
import com.hjq.base.BaseAdapter;
import com.hjq.widget.layout.SettingBar;
import com.lxj.xpopup.XPopup;
import com.qiantu.api.entity.LinkageBean;
import com.qiantu.api.entity.LinkageNewBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.bean.LocationData;
import com.qiantu.phone.ui.activity.map.LocationActivity;
import com.qiantu.phone.ui.adapter.ListLabelAdapter;
import com.qiantu.phone.ui.dialog.BottomDateDialog;
import com.qiantu.phone.ui.dialog.BottomDelayTimeDialog;
import com.qiantu.phone.ui.dialog.BottomListDialog;
import com.qiantu.phone.ui.dialog.BottomNormalTimeDialog;
import com.qiantu.phone.ui.dialog.BottomSunriseSunsetDialog;
import com.qiantu.phone.ui.dialog.BottomSunriseSunsetDialogNew;
import com.qiantu.phone.ui.dialog.BottomTimePeriodDialog;
import com.qiantu.phone.utils.LinkageDataHolder;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class SunriseSunSetActivityNew extends AppActivity {
    private SettingBar settingAction;
    private SettingBar settingTime;
    private SettingBar settingDate;
    private SettingBar settingReferenceArea;
    private CheckBox onlyBox;
    private CheckBox everydayBox;
    private CheckBox weekdayBox;
    private CheckBox weekendBox;
    private CheckBox weekBox;
    private CheckBox somedayBox;
    private View selectDayLayout;
    private View btnMonday;
    private View btnTuesday;
    private View btnWednesday;
    private View btnThursday;
    private View btnFriday;
    private View btnSaturday;
    private View btnSunday;
    private LinkageNewBean mLinkageBean;
    private LinkageNewBean.LinkageSunCondition mLinkageCondition;
    private BottomListDialog mBottomActionDialog;
    private BottomSunriseSunsetDialogNew mBottomSunriseDialog;
    private BottomNormalTimeDialog mBottomNormalTimeDialog;
    private BottomDateDialog mBottomDateDialog;
    private int[] mins = {-120,-60,-30,-10,-5,0,5,10,30,60,120};

    @Override
    protected int getLayoutId() {
        return R.layout.activity_sunrise_sunset_new;
    }

    @Override
    protected void initView() {
        settingAction = findViewById(R.id.setting_linkage_action);
        settingReferenceArea = findViewById(R.id.setting_reference_area);
        settingTime = findViewById(R.id.setting_linkage_time);
        onlyBox = findViewById(R.id.linkage_time_only_check);
        everydayBox = findViewById(R.id.linkage_time_everyday_check);
        weekdayBox = findViewById(R.id.linkage_time_weekday_check);
        weekendBox = findViewById(R.id.linkage_time_weekend_check);
        weekBox = findViewById(R.id.linkage_time_week_check);
        somedayBox = findViewById(R.id.linkage_time_someday_check);
        selectDayLayout = findViewById(R.id.select_day_layout);
        btnMonday = findViewById(R.id.btn_monday);
        btnTuesday = findViewById(R.id.btn_tuesday);
        btnWednesday = findViewById(R.id.btn_wednesday);
        btnThursday = findViewById(R.id.btn_thursday);
        btnFriday = findViewById(R.id.btn_friday);
        btnSaturday = findViewById(R.id.btn_saturday);
        btnSunday = findViewById(R.id.btn_sunday);
        settingDate = findViewById(R.id.linkage_time_begintime);
        setOnClickListener(settingAction,settingTime,btnMonday, btnTuesday, btnWednesday, btnThursday, btnFriday, btnSaturday, btnSunday,settingDate, onlyBox, everydayBox, weekdayBox, weekendBox, weekBox, somedayBox);
    }

    @Override
    protected void initData() {
        mLinkageBean = LinkageDataHolder.getLinkageDataNew();
        mLinkageCondition =  getSerializable("condition");
        if(mLinkageCondition == null){
            mLinkageCondition = new LinkageNewBean.LinkageSunCondition();
            mLinkageCondition.setConditionTypeId(6);
            mLinkageCondition.setRepeatTypeId(1);
            mLinkageCondition.setIcon("icon_intelligent_condition_timing");
            mLinkageCondition.setDirectionFromTypeId(1);
            mLinkageCondition.setTowardFromTypeId(1);
            mLinkageCondition.setTowardFromMinute(0);
            mLinkageCondition.setDirectionToTypeId(2);
            mLinkageCondition.setTowardToTypeId(1);
            mLinkageCondition.setTowardToMinute(0);
            mLinkageCondition.setActionTypeId(1);
            mLinkageCondition.setOperatorTypeId(1);
            List<Integer> weekDays = new ArrayList<>();
            for(int i = 0;i < 7;i++){
                weekDays.add(i);
            }
            mLinkageCondition.setRepeatWeekDays(weekDays);
            mLinkageCondition.setSort(mLinkageBean.getConditions().indexOf(mLinkageCondition) + 1);
        }
        updateView();
    }

    private void updateView(){
        setActionType();
        setTimeType(mLinkageCondition.getRepeatTypeId());
        btnMonday.setSelected(mLinkageCondition.getRepeatWeekDays().contains(1));
        btnTuesday.setSelected(mLinkageCondition.getRepeatWeekDays().contains(2));
        btnWednesday.setSelected(mLinkageCondition.getRepeatWeekDays().contains(3));
        btnThursday.setSelected(mLinkageCondition.getRepeatWeekDays().contains(4));
        btnFriday.setSelected(mLinkageCondition.getRepeatWeekDays().contains(5));
        btnSaturday.setSelected(mLinkageCondition.getRepeatWeekDays().contains(6));
        btnSunday.setSelected(mLinkageCondition.getRepeatWeekDays().contains(0));

        settingReferenceArea.setRightDrawable(AppApplication.getInstance().getThemeDrawable(R.attr.icon_list_next_normal));
        if(TextUtils.isEmpty(mLinkageCondition.getAreaName())){
            mLinkageCondition.setLongitude(AppApplication.getInstance().getCurHouseInfo().getLongitude());
            mLinkageCondition.setLatitude(AppApplication.getInstance().getCurHouseInfo().getLatitude());
            mLinkageCondition.setAreaName(AppApplication.getInstance().getCurHouseInfo().getProvince() + " " + AppApplication.getInstance().getCurHouseInfo().getCity() + " " + AppApplication.getInstance().getCurHouseInfo().getDistrict());
            mLinkageCondition.setAreaCode(AppApplication.getInstance().getCurHouseInfo().getDistrictId());
        }
        setOnClickListener(settingReferenceArea);

        settingReferenceArea.setRightText(mLinkageCondition.getAreaName());
    }

    private void setActionType() {
        String firstSunText;
        if(mLinkageCondition.getDirectionFromTypeId() == 1){
            firstSunText = getString(R.string.sunrise);
        }else{
            firstSunText = getString(R.string.sunset);
        }
        if(mLinkageCondition.getTowardFromMinute() == 0){
            firstSunText = firstSunText + getString(R.string.when_time);
        }else if(mLinkageCondition.getTowardFromTypeId() == 1){
            firstSunText = firstSunText + "前" + mLinkageCondition.getTowardFromMinute() + "分钟";
        }else if(mLinkageCondition.getTowardFromTypeId() == 2){
            firstSunText = firstSunText + "后" + mLinkageCondition.getTowardFromMinute() + "分钟";
        }
        if(mLinkageCondition.getActionTypeId() == 1){
            settingAction.setRightText( R.string.linkage_action_become);
            settingTime.setRightText(firstSunText);
        }else{
            String secondSunText;
            if(mLinkageCondition.getDirectionToTypeId() == 1){
                secondSunText = getString(R.string.sunrise);
            }else{
                secondSunText = getString(R.string.sunset);
            }
            if(mLinkageCondition.getTowardToMinute() == 0){
                secondSunText = secondSunText + getString(R.string.when_time);
            }else if(mLinkageCondition.getTowardToTypeId() == 1){
                secondSunText = secondSunText + "前" + mLinkageCondition.getTowardToMinute() + "分钟";
            }else if(mLinkageCondition.getTowardToTypeId() == 2){
                secondSunText = secondSunText + "后" + mLinkageCondition.getTowardToMinute() + "分钟";
            }
            settingAction.setRightText( R.string.linkage_action_keep);
            settingTime.setRightText(firstSunText + " - " + secondSunText);
        }
    }
    @Override
    public void onRightClick(View view) {
        mLinkageCondition.getRepeatWeekDays().clear();
        if(btnSunday.isSelected()){
            mLinkageCondition.getRepeatWeekDays().add(0);
        }
        if(btnMonday.isSelected()){
            mLinkageCondition.getRepeatWeekDays().add(1);
        }
        if(btnTuesday.isSelected()){
            mLinkageCondition.getRepeatWeekDays().add(2);
        }
        if(btnWednesday.isSelected()){
            mLinkageCondition.getRepeatWeekDays().add(3);
        }
        if(btnThursday.isSelected()){
            mLinkageCondition.getRepeatWeekDays().add(4);
        }
        if(btnFriday.isSelected()){
            mLinkageCondition.getRepeatWeekDays().add(5);
        }
        if(btnSaturday.isSelected()){
            mLinkageCondition.getRepeatWeekDays().add(6);
        }
        if(getBoolean("isAdd")){
            mLinkageBean.getConditions().add(mLinkageCondition);
        }else{
            int position = getInt("position");
            mLinkageBean.getConditions().remove(position);
            mLinkageBean.getConditions().add(position,mLinkageCondition);
        }
        startActivity_SingleTask(EditLinkageActivityNew.class);
        finish();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.setting_linkage_action:
                showBottomListDialog();
                break;
            case R.id.setting_linkage_time:
                showBottomTimePeriodDialog();
                break;
            case R.id.linkage_time_only_check:
                setTimeType(1);
                break;
            case R.id.linkage_time_everyday_check:
                setTimeType(2);
                break;
            case R.id.linkage_time_weekday_check:
                setTimeType(3);
                break;
            case R.id.linkage_time_weekend_check:
                setTimeType(4);
                break;
            case R.id.linkage_time_week_check:
                setTimeType(5);
                break;
            case R.id.linkage_time_someday_check:
                setTimeType(6);
                break;
            case R.id.linkage_time_begintime:
                showBottomDateDialog();
                break;
            case R.id.setting_reference_area:
//                showBottomDistrictDialog();
            Intent intent = new Intent(getContext(), LocationActivity.class);
            LocationData locationData = new LocationData();
            if(mLinkageCondition.getLongitude() > 0 && mLinkageCondition.getLatitude() > 0){
                locationData.location = mLinkageCondition.getLongitude() + ":" + mLinkageCondition.getLatitude();
            }else{
                if (AppApplication.getInstance().getCurHouseInfo() != null) {
                    locationData.location = AppApplication.getInstance().getCurHouseInfo().getLongitude() + ":" + AppApplication.getInstance().getCurHouseInfo().getLatitude();
                }
            }
            LocationActivity.startForResult(this, locationData, 1, LocationActivity.LOCATION_REQUEST_CODE);
            break;
            default:
                view.setSelected(!view.isSelected());
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK){
            if (requestCode == LocationActivity.LOCATION_REQUEST_CODE) {
                LocationData locationData = (LocationData) data.getSerializableExtra("DATA");
                mLinkageCondition.setLongitude(locationData.longitude);
                mLinkageCondition.setLatitude(locationData.latitude);
                mLinkageCondition.setAreaName(locationData.province + " " + locationData.city + " " + locationData.district);
                mLinkageCondition.setAreaCode(locationData.provinceId + locationData.cityId + locationData.districtId);
                settingReferenceArea.setRightText(mLinkageCondition.getAreaName());
            }
        }
    }

    private void setTimeType(int timeType) {
        mLinkageCondition.setRepeatTypeId(timeType);
        settingDate.setVisibility(timeType == 6 ? View.VISIBLE : View.GONE);
        selectDayLayout.setVisibility(timeType == 5 ? View.VISIBLE : View.GONE);
        onlyBox.setChecked(timeType == 1 ? true : false);
        everydayBox.setChecked(timeType == 2 ? true : false);
        weekdayBox.setChecked(timeType == 3 ? true : false);
        weekendBox.setChecked(timeType == 4 ? true : false);
        weekBox.setChecked(timeType == 5 ? true : false);
        somedayBox.setChecked(timeType == 6 ? true : false);
    }

    private void showBottomListDialog() {
        if (mBottomActionDialog == null) {
            mBottomActionDialog = new BottomListDialog(this);
            ListLabelAdapter listLabelAdapter = new ListLabelAdapter(this);
            List<String> data = new ArrayList<>();
            data.add(getString(R.string.linkage_action_become));
            data.add(getString(R.string.linkage_action_keep));
            listLabelAdapter.setData(data);
            listLabelAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
                    mBottomActionDialog.dismiss();
                    mLinkageCondition.setActionTypeId(position + 1);
                    setActionType();
                }
            });
            mBottomActionDialog.setAdapter(listLabelAdapter);
            new XPopup.Builder(this)
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(mBottomActionDialog);
        }
        ListLabelAdapter listLabelAdapter = (ListLabelAdapter) mBottomActionDialog.getAdapter();
        listLabelAdapter.setSelected("");
        mBottomActionDialog.show();
    }

    private void showBottomTimePeriodDialog(){
        if (mBottomSunriseDialog == null) {
            mBottomSunriseDialog = new BottomSunriseSunsetDialogNew(this);
            mBottomSunriseDialog.setDialogClickListener(new BottomSunriseSunsetDialogNew.DialogClickListener() {
                @Override
                public void onConfirmClick(int startTime, int endTime) {
                    mBottomSunriseDialog.dismiss();
                    mLinkageCondition.setDirectionFromTypeId(startTime/mins.length + 1);
                    int position = startTime % mins.length;
                    int minute  = mins[position];
                    if(minute > 0){
                        mLinkageCondition.setTowardFromTypeId(2);
                        mLinkageCondition.setTowardFromMinute(minute);
                    }else{
                        mLinkageCondition.setTowardFromTypeId(1);
                        mLinkageCondition.setTowardFromMinute(Math.abs(minute));
                    }
                    if(mLinkageCondition.getActionTypeId() == 2){
                        mLinkageCondition.setDirectionToTypeId(endTime/mins.length + 1);
                        int secPosition = endTime % mins.length;
                        int secMinute  = mins[secPosition];
                        if(minute > 0){
                            mLinkageCondition.setTowardToTypeId(2);
                            mLinkageCondition.setTowardToMinute(secMinute);
                        }else{
                            mLinkageCondition.setTowardToTypeId(1);
                            mLinkageCondition.setTowardToMinute(Math.abs(secMinute));
                        }
                    }
                    setActionType();
                }
            });
            new XPopup.Builder(this)
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(mBottomSunriseDialog);
        }
        mBottomSunriseDialog.setSingle(mLinkageCondition.getActionTypeId() == 1);
        mBottomSunriseDialog.show();
    }

    private void showBottomDateDialog() {
        if (mBottomDateDialog == null) {
            mBottomDateDialog = new BottomDateDialog(this);
            mBottomDateDialog.setDialogClickListener(new BottomDateDialog.DialogClickListener() {
                @Override
                public void onConfirmClick(String startTime, String endTime) {
                    settingDate.setRightText(startTime + " - " + endTime);
                    mLinkageCondition.setRepeatFromTime(startTime);
                    mLinkageCondition.setRepeatToTime(endTime);
                    mBottomDateDialog.dismiss();
                }
            });
            new XPopup.Builder(this)
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(mBottomDateDialog);
        }
        mBottomDateDialog.setSelectDate(mLinkageCondition.getRepeatFromTime(), mLinkageCondition.getRepeatToTime());
        mBottomDateDialog.show();
    }
}